﻿using Photon.Pun;
using Photon.Realtime;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public enum ChessType
{
    Write=-1,
    Black=1
}
public enum GameState
{
    Ready,
    Run,
    End
}
public enum GameMode
{
    StandAlone,
    Internet

}
public class ChessController : MonoBehaviour
{
    public static int h_count = 15;
    private RectTransform chessMap;
    public float xOffset;
    public Vector3[,] chessPos = new Vector3[h_count, h_count];
    public GameObject WriteChess;
    public GameObject blackChess;
    public float GridWeight;
    public int[,] chessDic =new int[h_count, h_count];
    private float leftx;
    private float lefty;
    private float rightx;
    private float righty;
    public GameState gameState;
    public GameMode gameMode = GameMode.Internet;

    public static Action ready;
    public static Action run;
    public static Action<ChessType> win;
    private Transform chessRoot;
    public List<GameObject> writepool = new List<GameObject>();
    public List<GameObject> blackpool = new List<GameObject>();
    int step = 0;
    public bool IsPingJu
    {
        get
        {
            if (step>=225)
            {
                return true;
            }
            return false;
        }
    }
    void Awake()
    {
        chessMap =transform.Find("Map").GetComponent<RectTransform>();
        chessRoot = transform.Find("Chess").GetComponent<Transform>();
        InitChessMap();
        
    }
    void Start()
    {
        SetGameState(GameState.Ready);
    }

  

    public void SetGameState(GameState gameState)
    {
        switch (gameState)
        {
            case GameState.Ready:
                if (ready!=null)
                {
                    ready();
                }
                InitChessState();
                break;
            case GameState.Run:
                if (run!=null)
                {
                    run();
                }
                break;
            case GameState.End:

                break;
        }
        this.gameState = gameState;
    }



    
    public void InitChessState()
    {
        for (int i = 0; i < h_count; i++)
        {
            for (int j = 0; j < h_count; j++)
            {
                chessDic[i, j] = 0;
            }
        }
        for (int i = 0; i < writepool.Count; i++)
        {
            if (writepool[i].activeInHierarchy)
            {
                writepool[i].SetActive(false);
            }
        }
        for (int i = 0; i < blackpool.Count; i++)
        {
            if (blackpool[i].activeInHierarchy)
            {
                blackpool[i].SetActive(false);
            }
        }
    }


    public void InitChessMap()
    {
        Vector3[] v = new Vector3[4];
        chessMap.GetWorldCorners(v);
        leftx = v[0].x + xOffset;
        lefty = v[0].y + xOffset;
        float chessWeight = v[3].x - v[0].x - 2 * xOffset;
        rightx = leftx + chessWeight;
        righty = lefty + chessWeight;
        GridWeight = (chessWeight) / (h_count - 1);
        for (int i = 0; i < 15; i++)
        {
            for (int j = 0; j < 15; j++)
            {
                Vector3 pos = new Vector3(leftx + GridWeight * i, lefty + GridWeight * j, v[0].z);
                chessPos[i, j] = pos;
            }
        }
        
    }

    public GameObject GetChess(ChessType chess)
    {
        switch (chess)
        {
            case ChessType.Write:
                for (int i = 0; i < writepool.Count; i++)
                {
                    if (!writepool[i].activeInHierarchy)
                    {
                        return writepool[i];
                    }
                }
                break;
            case ChessType.Black:
                for (int i = 0; i < blackpool.Count; i++)
                {
                    if (!blackpool[i].activeInHierarchy)
                    {
                        return blackpool[i];
                    }
                }
                break;
        }
        return null;
    }
    public void CreateChess(int x,int y,ChessType chessType)
    {
        Vector3 pos = chessPos[x, y];
        GameObject chess = GetChess(chessType);
        switch (chessType)
        {
            case ChessType.Write:
                if (chess == null)
                {

                     chess = GameObject.Instantiate(Resources.Load<GameObject>("chessWritw"), pos, Quaternion.identity, chessRoot);
                            //chess.transform.SetParent(chessRoot);
                    writepool.Add(chess);
                }

                chessDic[x, y] = -1;
                break;
            case ChessType.Black:
                if (chess == null)
                {
                    chess = GameObject.Instantiate(Resources.Load<GameObject>("chessBlack"), pos, Quaternion.identity,chessRoot);
                    //chess.transform.SetParent(chessRoot);
                    blackpool.Add(chess);
                }
                chessDic[x, y] = 1;
                break;
        }
        chess.transform.position = pos;
        chess.SetActive(true);
        step += 1;
    }

    public bool ClimpMousePos(Vector3 inputPos,ref int xInt, ref int yInt)
    {
        if (inputPos.x<(leftx-GridWeight*0.5f)||inputPos.x>rightx+(GridWeight * 0.5f) || inputPos.y < lefty-(GridWeight * 0.5f) || inputPos.y > righty+(GridWeight * 0.5f))
        {
            return false;
        }
        float weight= inputPos.x - leftx;
        float height= inputPos.y - lefty;
        float x=weight / GridWeight;
        float y= height / GridWeight;
        xInt = Mathf.RoundToInt(x);
        yInt = Mathf.RoundToInt(y);
        if (chessDic[xInt,yInt]!=0)
        {
            return false;
        }
        return true;
    }

    public bool IsWin(int ChessType)
    {
        bool win = false;
      
        for (int i = 0; i < h_count-4; i++)
        {
            for (int j = 0; j < h_count; j++)
            {
                if (j < 4)
                {
                    //横向
                    if (chessDic[i,j] == ChessType && chessDic[i, j + 1] == ChessType && chessDic[i, j + 2] == ChessType && chessDic[i, j + 3] == ChessType && chessDic[i, j + 4] == ChessType)
                    {
                        return true;
                    }
                    //纵向
                    if (chessDic[i, j] == ChessType && chessDic[i + 1, j] == ChessType && chessDic[i + 2, j] == ChessType && chessDic[i + 3, j] == ChessType && chessDic[i + 4, j] == ChessType)
                    {
                        return true;
                    }
                    //右斜线
                    if (chessDic[i, j] == ChessType && chessDic[i + 1, j + 1] == ChessType && chessDic[i + 2, j + 2] == ChessType && chessDic[i + 3, j + 3] == ChessType && chessDic[i + 4, j + 4] == ChessType)
                    {
                        return true;
                    }
                }
                else if (j >= 4 && j < 11)
                {
                    //横向
                    if (chessDic[i, j] == ChessType && chessDic[i, j + 1] == ChessType && chessDic[i, j + 2] == ChessType && chessDic[i, j + 3] == ChessType && chessDic[i, j + 4] == ChessType)
                    {
                        return true;
                    }
                    //纵向
                    if (chessDic[i, j] == ChessType && chessDic[i + 1, j] == ChessType && chessDic[i + 2, j] == ChessType && chessDic[i + 3, j] == ChessType && chessDic[i + 4, j] == ChessType)
                    {
                        return true;
                    }
                    //右斜线
                    if (chessDic[i, j] == ChessType && chessDic[i + 1, j + 1] == ChessType && chessDic[i + 2, j + 2] == ChessType && chessDic[i + 3, j + 3] == ChessType && chessDic[i + 4, j + 4] == ChessType)
                    {
                        return true;
                    }
                    //左斜线
                    if (chessDic[i, j] == ChessType && chessDic[i + 1, j - 1] == ChessType && chessDic[i + 2, j - 2] == ChessType && chessDic[i + 3, j - 3] == ChessType && chessDic[i + 4, j - 4] == ChessType)
                    {
                        return true;
                    }
                }
                else
                {
                    //横向
                    //if (chessState[i, j] == 1 && chessState[i, j + 1] == 1 && chessState[i, j + 2] == 1 && chessState[i, j + 3] == 1 && chessState[i, j + 4] == 1)
                    //{
                    //    flag = 1;
                    //    return flag;
                    //}
                    //纵向
                    if (chessDic[i, j] == ChessType && chessDic[i + 1, j] == ChessType && chessDic[i + 2, j] == ChessType && chessDic[i + 3, j] == ChessType && chessDic[i + 4, j] == ChessType)
                    {
                        return true;
                    }
                    //右斜线
                    //if (chessState[i, j] == 1 && chessState[i + 1, j + 1] == 1 && chessState[i + 2, j + 2] == 1 && chessState[i + 3, j + 3] == 1 && chessState[i + 4, j + 4] == 1)
                    //{
                    //    flag = 1;
                    //    return flag;
                    //}
                    //左斜线
                    if (chessDic[i, j] == ChessType && chessDic[i + 1, j - 1] == ChessType && chessDic[i + 2, j - 2] == ChessType && chessDic[i + 3, j - 3] == ChessType && chessDic[i + 4, j - 4] == ChessType)
                    {
                        return true;
                    }
                }
            }
        }
        return win;
    }
}